package com.yeren.servlet.tarena.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoadEmpServlet extends HttpServlet {
	/*
	 * 使用请求中的name,contact两组参数值 分别使用getParameter和getParameterValues方法获取
	 */
	public void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 保证正确输出中文
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		//
		int id=Integer.parseInt(req.getParameter("id"));
		// 
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs=null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			conn = DriverManager
					.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
							"scott", "tiger");
			stat = conn.prepareStatement("select * from liu_temp where id=?");
			stat.setInt(1,id);
			rs=stat.executeQuery();
			out.println("<html><head></head><body style='font-size:30px'>");
			if(rs.next()){
				String name=rs.getString("name");
				double salary=rs.getDouble("salary");
				int age=rs.getInt("age");
				out.println("<form action='modify' method='post'>");
				out.println("编号："+id+"<br/>");
				out.println("<input type='hidden' name='id' value='"+id+"'/><br/>");
				out.println("姓名：<input name='name' value='"+name+"'/><br/>");
				out.println("薪水：<input name='salary' value='"+salary+"'/><br/>");
				out.println("年龄：<input name='age' value='"+age+"'/><br/>");
				out.println("<input type='submit' value='修改' />");
				out.println("</form>");
			}
			out.println("</BODY>");
			out.println("</HTML>");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("系统繁忙，稍后重试");
		} finally {         
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
}
